package com.qf.blog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.blog.commons.utils.ConvertUtils;
import com.qf.blog.dto.PostDTO;
import com.qf.blog.entity.Comment;
import com.qf.blog.mapper.PostMapper;
import com.qf.blog.mapper.UserMapper;
import com.qf.blog.service.ISysPostService;
import com.qf.blog.vo.LikeVo;
import com.qf.blog.vo.PostVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SysPostServiceImpl extends ServiceImpl<PostMapper, PostVo> implements ISysPostService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommentServiceImpl commentService;
    @Autowired
    private PostServiceImpl postService;
    @Autowired
    private LikeServiceImpl likeService;

    @Override
    public List<PostDTO> queryAll() {
        List<PostVo> list = super.list();
        List<PostDTO> postDTOS = list.stream().map(postService::postVoMapper).map((item) -> {
            Long count = commentService.lambdaQuery().eq(Comment::getPostId, item.getId()).count();
            PostDTO postDTO = ConvertUtils.sourceToTarget(item, PostDTO.class);
            postDTO.setCommentCount(Integer.valueOf(count.toString()));
            LikeVo likeVo = new LikeVo();
            likeVo.setEntityId(item.getId());
            likeVo.setEntityType(1);
            // 3.获取帖子的点赞数量
            Long likeCount = likeService.getLikeCount(likeVo);
            postDTO.setLikeCount(Integer.valueOf(likeCount.toString()));
            return postDTO;
        }).collect(Collectors.toList());
        return postDTOS;
    }

}
